<?php

class ContactController extends Zend_Controller_Action
{

    public function init()
    {
        /* Initialize action controller here */
        require_once "models/Db/Event_db.php";
        require_once "models/Utility/Utility.php";

        $utility_handle = new Utility();
        $eventHandle    = new Event_db();
        
                $pivaList = $eventHandle->getAllPiva();
                foreach ($pivaList as $pivaList2){
                        $piva = $pivaList2['piva'];
                        $temp = $eventHandle->getUniqueNamebyPiva($piva);
                        $allDisco[$piva] = array ($temp['unique_name'], $temp['name']);
                }
                $allDisco = $utility_handle->subval_sort($allDisco,0);
                $this->view->allDisco = $allDisco;
        
                foreach ($pivaList as $pivaList2){
                    $piva = $pivaList2['piva'];
                    $temp = $eventHandle->getPaeseDsicoByPiva($piva);
                    $paese[$piva] = $temp['paese'];
                    //$allDisco[] = $temp['name'];
                    //$i++;
                }
                $this->view->paese = array_unique($paese);
        
        
                $categoryList = $eventHandle->getAllCategory();
                $this->view->category = $categoryList;
        
                $zonaList = array('Brescia','Verona','Mantova','Cremona');
                $this->view->zonaList = $zonaList;
        
                $this->_helper->_layout->setLayout('about');
                $div = '<div id="box">';
                $end_div = "</div>";
                $contatti ='<h2 class="about">Contatti</h2>';
        
                $mail_support = '<p>Supporto tecnico: '.
                                '<a class="about_mail" href="mailto:help@whichclub.it?subject=help">help@whichclub.it</a></p>';
                $mail_help = '<p>Help prenotazioni:<br />' .
                             '<a class="about_mail" href="mailto:help@whichclub.it?subject=help">help@whichclub.it</a></p>';
                $mail_info = '<p>Richieste generali:<br />' .
                             '<a class="about_mail" href="mailto:info@whichclub.it?subject=Informazioni">info@whichclub.it</a></p>';
                $mail_feed = '<p>Feedback:<br />' .
                             '<a class="about_mail" href="mailto:social@whichclub.it?subject=Informazioni">social@whichclub.it</a></p>';
                $tel = '<p class="about_tel1">Info:<br />Notty<br /> +39 338 56 07 826<br /><br />Supporto tecnico:</p><p class="about_tel2">Francesco<br />+39 340 27 39 492<br />Jacopo<br />+39 340 61 92 148</p>';
        
                $this->view->layout()->sponsor2 = $div.$contatti.$mail_info.$mail_support.$mail_help.$mail_feed.$tel.$end_div;
    }

    public function indexAction()
    {
        // action body
    }

    protected function _process($values)
    {
        $mail = $values['email'];
                $comment = $values['comment'];
                $phone = $values['phone'];
    
                $data = array('email' => $mail,
                              'note'=>$comment,
                              'phone'=>$phone);

                try{
   
                    require 'models/Db/NewsLetter_db.php';
                    $db_obj = new NewsLetter_db();
                    $db_obj->saveContactInfo($data);
                    return true;
                    
                } catch (Zend_Mail_Exception $e){
                    echo $e->getMessage();
                    return false;
                }
    }

    public function informazioniAction()
    {
        // action body
                $request = $this->getRequest();
                $form    = new Application_Form_Info();

                if ($this->getRequest()->isPost()) {
                    if ($form->isValid($request->getPost())) {

                        if ($this->_process($form->getValues())) {
                            $urlOptions = array('controller'=>'Contact', 'action'=>'success');
                            $this->_helper->redirector->gotoRoute($urlOptions,'contact');
                        }else{
                            $urlOptions = array('controller'=>'Contact', 'action'=>'informazioni');
                            $this->_helper->redirector->gotoRoute($urlOptions,'contact');
                        }
                    }
                }
                $this->view->form = $form;
                //return $this->_helper->redirector('index');
    }

    public function privacyPolicyAction()
    {
        // action body
    }

    protected function _process_serata($values)
    {
        $mail = $values['email'];
                $name = $values['name'];
                $phone = $values['phone'];
                $note = $values['note'];
                $festa = $values['festa'];
                $data = $values['data_richiesta'];
        
                $data = array(
                    'email'=>$mail,
                    'nome'=>$name,
                    'phone'=>$phone,
                    'note'=>$note,
                    'tipo_festa'=>$festa,
                    'data_richiesta'=>$data
                );
        
                try{
        
                    require 'models/Db/NewsLetter_db.php';
                    $db_obj = new NewsLetter_db();
                    $db_obj->saveSerata($data);
                    return true;
        
                } catch (Zend_Mail_Exception $e){
                    echo $e->getMessage();
                    return false;
                }
    }

    public function organizzaSerataAction()
    {
        // action body
                $request = $this->getRequest();
                $form    = new Application_Form_OrganizzaSerata();
        
                if ($this->getRequest()->isPost()) {
                    if ($form->isValid($request->getPost())) {
        
                        if ($this->_process($form->getValues())) {
                            var_dump($this->_process_serata($form->getValues()));
                            $urlOptions = array('controller'=>'Contact', 'action'=>'success');
                            $this->_helper->redirector->gotoRoute($urlOptions,'contact');
                        }else{
                            $urlOptions = array('controller'=>'Contact', 'action'=>'organizza-serata');
                            var_dump($this->_process_serata($form->getValues()));
                            $this->_helper->redirector->gotoRoute($urlOptions,'contact');
                        }
                    }
                }
                $this->view->form = $form;
    }

    public function pressAction()
    {
        // action body
    }

    public function prenotaTavoloAction()
    {
        // action body
        $request = $this->getRequest();
        $form    = new Application_Form_Prenotatavolo();

        if ($this->getRequest()->isPost()) {
            if ($form->isValid($request->getPost())) {

                if ($this->_process_tavolo($form->getValues())) {
                    $urlOptions = array('controller'=>'Contact', 'action'=>'success');
                    $this->_helper->redirector->gotoRoute($urlOptions,'contact');
                }else{
                    $urlOptions = array('controller'=>'Contact', 'action'=>'prenotatavolo');
                    $this->_helper->redirector->gotoRoute($urlOptions,'contact');
                }
            }
        }
        $this->view->form = $form;

    }

    public function _process_tavolo($values){

            $mail = $values['email'];
            $name = $values['name'];
            $phone = $values['phone'];
            $botiglie = $values['numero_bottiglie'];
            $persone = $values['numero_persone'];
            $tavoli = $values['numero_tavoli'];
            $note = $values['note'];
            $locale = $values['locale'];
            $data = $values['data_richiesta'];

      
            $configInfo = array('auth'=>'login', 'username'=>'info@whichclub.it','password'=>'********');
            $smtpHost = new Zend_Mail_Transport_Smtp('smtp.whichclub.it',$configInfo);
      
            $MailObj = new Zend_Mail();

            $emailfooter="<br /><h6>Nel caso ci fossero degli errori non esitare a contattarci ("
                            . "<a href='http://www.whichclub.it/contatti'>conttatti)</a>!"
                            . "<br />Per qualsiasi informazione: info@whichlcub.it<br />"
                            . "All rigth reserved @whichclub.it - Booking Events</h6>";

            $emailMessage="<h3>Riepilogo prenotazione:</h3>".
                            "<table>".
                                "<tr><td>Name:</td><td>" 		. $name     . "</td></tr>".
                                "<tr><td>Telefono:</td><td>"    . $phone    . "</td></tr>".
                                "<tr><td>Mail:</td><td>"        . $mail     . "</td></tr>".
                                "<tr><td>Tavoli:</td><td>"      . $tavoli   . "</td></tr>".
                                "<tr><td>Persone:</td><td>"     . $persone  . "</td></tr>".
                                "<tr><td>Bottiglie:</td><td>"   . $botiglie . "</td></tr>".
                                "<tr><td>data:</td><td>"        . $data     . "</td></tr>".
                                "<tr><td>locale:</td><td>"      . $locale   . "</td></tr>".
                                "<tr><td>note:</td><td>"        . $note     . "</td></tr>".
                            "</table>".$emailfooter;

            $fromEmail="info@whichclub.it";
            $fromFullName="whichclub - booking events";
            $to = $mail;
            $bcc = "info@whichclub.it";
            $subject ="Prenotazione tavolo";

            $MailObj = new Zend_Mail();
            $MailObj->setBodyHtml($emailMessage);
            $MailObj->setFrom($fromEmail,$fromFullName);
            $MailObj->addTo($to);
            $MailObj->addBcc($bcc);
            $MailObj->setSubject($subject);
        

            try{
                $MailObj->send($smtpHost);
                if(!$MailObj){
                    return false;
                }
                return true;

            } catch (Zend_Mail_Exception $e){
                echo $e->getMessage();
                return false;
            }
    }
    
    public function successAction(){
        /*
        $id = $this->_getParam('id', 'err');
        var_dump($id);
        if ($id == 'err'){
            $this->view->messaggio = "Abbiamo riscontrato un errore. Torna indietro e riprova.";
        }elseif($id==1){
            $this->view->messaggio = "La tua richiesta &egrave; stata processata con successo.<br />Ti abbiamo invito una mail con il riepilogo della tua prenotazione.";
        }elseif($id==2){
            $this->view->messaggio = "La tua richiesta &egrave; stata processata con successo.<br />Ti contatteremo al pi&ugrave; presto.";
        }elseif($id==3){
            $this->view->messaggio = "La tua richiesta &egrave; stata processata con successo.<br />Ti contatteremo al pi&ugrave; presto.";
        }
         *
         */
        $this->view->messaggio = "La tua richiesta &egrave; stata processata con successo.<br />Ti contatteremo al più presto all'indirizzo e-mail che ci hai lasciato.";
    }    

}
